package com.guizi.halftimesroom.frament;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;
import com.guizi.halftimesroom.R;
import com.guizi.halftimesroom.bean.DiscoverRecommandBannerBean;
import com.guizi.halftimesroom.bean.DiscoverRecomnandTagBean;
import com.guizi.halftimesroom.bean.RecommandEventBean;
import com.guizi.halftimesroom.bean.RecommandListNormanlBean;
import com.guizi.halftimesroom.bean.RecommandListSpecialBean;
import com.guizi.halftimesroom.bean.RecommandSquareBean;
import com.guizi.halftimesroom.constant.UrlString;
import com.guizi.halftimesroom.utils.OkhttpUtil;
import com.guizi.halftimesroom.utils.RecommandJsonListener;
import com.guizi.halftimesroom.utils.RecommandJsonUtil;
import com.guizi.halftimesroom.utils.ResponseListener;
import com.guizi.halftimesroom.view.DIscoverBottomView;
import com.guizi.halftimesroom.view.RecommandEventsView;
import com.guizi.halftimesroom.view.RecommandSquareView;
import com.guizi.halftimesroom.view.RecommandTagView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRecommandFragment extends Fragment {
    private FlashView fv_banner;
    private LinearLayout ll_recommand_container;
    private List<String> bannerUrls;
    private Activity mActivity;
    private RecommandTagView tagView;
    private DIscoverBottomView bottomView;
    private RecommandEventsView eventsView;
    private RecommandSquareView squareView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bannerUrls = new ArrayList<>();
        mActivity = getActivity();
        tagView = new RecommandTagView(mActivity);
        eventsView = new RecommandEventsView(mActivity);
        squareView = new RecommandSquareView(mActivity);
        bottomView = new DIscoverBottomView(mActivity);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ;
        return inflater.inflate(R.layout.fragment_discover_banner, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        fv_banner = (FlashView) view.findViewById(R.id.fv_banner);
        ll_recommand_container = (LinearLayout) view.findViewById(R.id.ll_recommand_container);
            ll_recommand_container.addView(tagView);
            ll_recommand_container.addView(eventsView);
            ll_recommand_container.addView(squareView);
            ll_recommand_container.addView(bottomView);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setBanner();
        setTag();
        setEvent();
        setSquare();
        setBottom();
    }


    private void setBanner() {
        OkhttpUtil.loadData(mActivity, UrlString.DISCOVER_RECOMMAND_BANNER_URL, new ResponseListener() {
            @Override
            public void onFilure() {
            }

            @Override
            public void onSuccess(String s) {
                DiscoverRecommandBannerBean bean = new Gson().fromJson(s, DiscoverRecommandBannerBean.class);
                for (int i = 0; i < bean.getData().size(); i++) {
                    bannerUrls.add(bean.getData().get(i).getCover());
                }
                fv_banner.setImageUris(bannerUrls);
            }
        });
    }

    private void setTag() {

        OkhttpUtil.loadData(mActivity, UrlString.DISCOVER_RECOMMAND_TAG_URL, new ResponseListener() {
            @Override
            public void onFilure() {
            }

            @Override
            public void onSuccess(String s) {
                DiscoverRecomnandTagBean bean = new Gson().fromJson(s, DiscoverRecomnandTagBean.class);
                tagView.setData(bean.getData());
            }
        });
    }

    private void setEvent() {
        OkhttpUtil.loadData(mActivity, UrlString.DISCOVER_RECOMMAND_ACTION_URL, new ResponseListener() {
            @Override
            public void onFilure() {
            }

            @Override
            public void onSuccess(String s) {
                RecommandEventBean eventBean = new Gson().fromJson(s, RecommandEventBean.class);
                eventsView.setData(eventBean.getData());
            }
        });
    }


    private void setSquare() {
        OkhttpUtil.loadData(mActivity, UrlString.DISCOVER_RECOMMAND_RECENT_URL, new ResponseListener() {
            @Override
            public void onFilure() {
            }

            @Override
            public void onSuccess(String s) {
                RecommandSquareBean squareBean = new Gson().fromJson(s, RecommandSquareBean.class);
                squareView.setData(squareBean.getData());
            }
        });
    }

    private void setBottom() {
        RecommandJsonUtil.getData(mActivity, UrlString.DISCOVER_RECOMMAND_SELECTED_URL, new RecommandJsonListener() {
            @Override
            public void onFilure() {
            }

            @Override
            public void onSuccess(List<RecommandListNormanlBean> list, RecommandListSpecialBean specialBean) {
                bottomView.setData(list, specialBean);
            }
        });
    }


}
